package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;
    @Autowired
    private VodService vodService;
    @Autowired
    private TrackInfoMapper trackInfoMapper;

    /**
     * 上传曲目
     *
     * @param file 文件
     * @return {@link Result }<{@link Map }<{@link String }, {@link String }>>
     */
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(MultipartFile file) {
        Map<String, String> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    @GuiGuLogin
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }

    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable Integer page,
                                                       @PathVariable Integer limit,
                                                       @RequestBody TrackInfoQuery trackInfoQuery) {
        Page<TrackListVo> pageModel = trackInfoService.findUserTrackPage(page, limit, trackInfoQuery);
        return Result.ok(pageModel);
    }

    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoMapper.selectById(id);
        TrackInfo byId = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @RequestBody TrackInfoVo trackInfoVo) {

        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {

        trackInfoService.removeById1(id);

        return Result.ok();
    }

    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

    @GuiGuLogin(required = false)
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
                                                             @PathVariable Integer page,
                                                             @PathVariable Integer limit) {

        Long userId = AuthContextHolder.getUserId();
        Page<AlbumTrackListVo> pageModel = trackInfoService.findAlbumTrackPage(albumId, page, limit, userId);
        return Result.ok(pageModel);
    }

    @GuiGuLogin
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {

        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> map = trackInfoService.findUserTrackPaidList(trackId, userId);
        return Result.ok(map);
    }

    @GuiGuLogin
    @GetMapping("/trackInfo/getavalibleTrackIfo/{trackId}/{count}")
    public Result<List<TrackInfo>> getavalibleTrackIfo(@PathVariable Long trackId, @PathVariable Integer count) {
        Long userId = AuthContextHolder.getUserId();
        List<TrackInfo> trackInfo = trackInfoService.getavalibleTrackIfo(count, trackId);
        return Result.ok(trackInfo);
    }

}

